//
//  ViewController.m
//  11208
//
//  Created by student on 16/3/3.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong,nonatomic) NSArray *cars;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"];
    self.cars = [NSArray arrayWithContentsOfFile:path];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *keys = self.cars[section][@"cars"];
    return keys.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    NSDictionary *keys = self.cars[indexPath.section];
    NSString *img = keys[@"cars"][indexPath.row][@"icon"];
    NSLog(@"图片%@",img);
    cell.imageView.image = [UIImage imageNamed:img];
    NSString *txt = keys[@"cars"][indexPath.row][@"name"];
    cell.textLabel.text = txt;
    return cell;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.cars.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *keys = self.cars[section][@"title"];
    return keys;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *keys = [[NSMutableArray alloc]init];
    for (NSDictionary *tmp in self.cars)
    {
        [keys addObject: tmp[@"title"]];
    }
    return keys;
}

@end
